﻿/*************************************************************************
 *
 * File: remotedesktop.js
 *
 * Description: 
 * 
 ************************************************************************/

// Member variables.
var rdp_settings = {'servers' : []}

function Init()
{
	try
	{	
		// Enable the gadget settings functionality. 
		System.Gadget.settingsUI = "settings.html";
		System.Gadget.onSettingsClosed = SettingsClosed;
		
		// Specify the flyout root.
		System.Gadget.Flyout.file = "flyout.html";    
		
		//System.Gadget.onDock = OnDock;
		//System.Gadget.onUndock = OnUnDock;
		
		// Retrieve existing list of rdp servers
		var rdp_settings_json = System.Gadget.Settings.readString("rdp_settings_json");
		if (rdp_settings_json)
		{
			rdp_settings = JSON.parse(rdp_settings_json);
			System.Debug.outputString("RemoteDesktop.Init:rdp_settings = " + rdp_settings);
			System.Debug.outputString("RemoteDesktop.Init:rdp_settings.servers.length = " + rdp_settings.servers.length);	
			System.Debug.outputString("RemoteDesktop.Init:rdp_settings.servers[0].displayName = " + rdp_settings.servers[0].displayName);
		}	
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:RemoteDesktop.Init: " + e.description);
    }
}		

// --------------------------------------------------------------------
// Handle the Settings dialog closed event.
// event = Event argument.
// --------------------------------------------------------------------
function SettingsClosed(event)
{
	try
    {
		if (event.closeAction == event.Action.commit)   
		{ 
			// Retrieve existing list of rdp servers
			var rdp_settings_json = System.Gadget.Settings.readString("rdp_settings_json");
			if (rdp_settings_json)
			{
				rdp_settings = JSON.parse(rdp_settings_json);
				System.Debug.outputString("RemoteDesktop.SettingsClosed:rdp_settings = " + rdp_settings);
				if (rdp_settings.servers && rdp_settings.servers.length > 0)
				{
					System.Debug.outputString("RemoteDesktop.SettingsClosed:rdp_settings.servers.length = " + rdp_settings.servers.length);	
					System.Debug.outputString("RemoteDesktop.SettingsClosed:rdp_settings.servers[0].displayName = " + rdp_settings.servers[0].displayName);
				}
			}	
		}
	  }
    catch(e)
    {
        System.Debug.outputString("ERROR:RemoteDesktop.SettingsClosed: " + e.description);
    }
}

function ToggleFlyout()
{
    System.Gadget.Flyout.show = !System.Gadget.Flyout.show;
}

// function OnUnDock()
// {
	
// }

// function OnDock()
// {
	
// }